"""
update_info :
- 修改json存储内容
    update_time :2021年 08月 26日 星期四 16:52:20 CST
    {1} ItemAdapter(item).asdict() 等同于 list(self.file.read())
    {2} 英文编码 json.dumps(value)
    {3} 中文编码 son.dumps(value, ensure_ascii=False)
"""
import csv
import datetime
import json

from itemadapter import ItemAdapter


class JsonPipeline:
    def __init__(self):
        self.file = None
        self.scrapy_name = None
        self.file_name = None
        self.file_start_time = datetime.datetime.now()

    def process_item(self, item, spider):
        self.file.write(json.dumps(ItemAdapter(item).asdict(), ensure_ascii=False) + "\n") if item[
                                                                                                  "joke_text"] is not None else None
        return item

    def open_spider(self, spider):
        self.scrapy_name = spider.name
        self.file_name = f"{self.scrapy_name}_{self.file_start_time}"
        self.file = open(f'file/json/{self.file_name}.jl', 'w')

    def close_spider(self, spider):
        self.file.close()


class CsvPipeline(object):
    def __init__(self):
        self.file = None
        self.scrapy_name = None
        self.file_name = None
        self.file_start_time = datetime.datetime.now()
        self.writer = None

    def open_spider(self, spider):
        self.scrapy_name = spider.name
        self.file_name = f"{self.scrapy_name}_{self.file_start_time}"
        self.file = open(f'file/csv/{self.file_name}.csv', 'w')
        self.writer = csv.writer(self.file)
        self.writer.writerow(("joke_controller", "joke_text", "joke_create_time"))  # 写入表头

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        self.writer.writerow((item["joke_controller"], item["joke_text"], item["joke_create_time"])) if item[
                                                                                                            "joke_text"] is not None else None
        return item
